



# coding:utf-8
import datetime
import os
import shutil
import time

import allure
import pytest
from urllib3.exceptions import MaxRetryError

from Base.DriverCreat import DriverCreat
from Base.SerialCom import SerialCom
from vo.action.AylaHomeAction import AylaHomeAction

phone_driver, test_name = None, None

"""
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    rep = outcome.get_result()
    print(phone_driver)
    if rep.when == "call" and rep.failed:
        phone_driver.save_screenshot("ERROR.png")
        allure.attach.file("ERROR.png", attachment_type=allure.attachment_type.PNG, name='ERROR')
        os.system('rm -f ' + "ERROR.png ")

"""

def pytest_sessionfinish(session):
    print(11111)
    root_dir = session.config.rootdir
    result_folder = "/report/allure/" + datetime.datetime.now().strftime('%Y-%m-%d_%H_%M_%S')
    os.system("allure generate -o {folder}/html/ {result} ".format(folder=root_dir + "/" + result_folder,
                                                   result='tmp/'))
    shutil.rmtree("tmp/".format(folder=root_dir))



@pytest.fixture()
def aylaHome():
    aylaHomeAction=AylaHomeAction(driver=phone_driver)
    yield aylaHomeAction


@pytest.fixture(scope="module" , autouse=True)
def driver_init():
    global phone_driver
    connect=True
    try:
        _driver = DriverCreat()
    except ConnectionRefusedError:
        print('连接异常、可能未连接手机')
        connect = False
    except MaxRetryError:
        print('MaxRetryError')
        connect= False
    except :
        print('其他异常')
    else:
        phone_driver = _driver.init_webdriver()
    yield phone_driver
    print(1123)
    if connect:
        time.sleep(5)
        _driver.quit()





@pytest.fixture()
def serialCom():
    yield SerialCom()







